<template>
  <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    <el-form-item label="轮播封面" prop="poster">
      <ImgUpload v-model:poster="ruleForm.poster" />
    </el-form-item>
    <el-form-item label="轮播链接" prop="link">
      <el-input v-model="ruleForm.link" />
    </el-form-item>
    <el-form-item label="是否上架" prop="online">
      <el-switch v-model="ruleForm.online" />
    </el-form-item>
    <el-form-item label="轮播描述" prop="desc">
      <el-input v-model="ruleForm.desc" type="textarea" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm"> Create </el-button>
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import axios from "axios";
import ImgUpload from "@/components/img-upload.vue";
export default {
  components: {
    ImgUpload,
  },
  data() {
    return {
      ruleForm: {
        poster: "",
        link: "http://www.1000phone.com",
        online: 1,
        desc: "价值100万的轮播",
      },
      rules: {
        poster: [{ required: true, message: "请填写轮播图", trigger: "blur" }],
        link: [{ required: true, message: "请填写链接", trigger: "blur" }],
        online: [
          { required: true, message: "请填选择online", trigger: "blur" },
        ],
        desc: [{ required: true, message: "请填描述", trigger: "blur" }],
      },
    };
  },
  methods: {
    async submitForm() {
      console.log(this.$refs.ruleFormRef);
      let formEl = this.$refs.ruleFormRef; //获取了form表单对象
      if (!formEl) return;
      //触发表单验证
      await formEl.validate((valid, fields) => {
        if (valid) {
          console.log("submit!", this.ruleForm);
          axios({
            url: "https://2ynniqnv.lc-cn-n1-shared.com/1.1/classes/VueBanner",
            method: "POST",
            headers: {
              "X-LC-Id": "2YNNiQnvnPyOkdbntRcixlTr-gzGzoHsz", //务必用自己的ID
              "X-LC-Key": "SO6WYaO2SRHMRiIOdEsdwUok", //务必用自己的Key
              "Content-Type": "application/json",
            },
            data: this.ruleForm,
          });
        } else {
          console.log("error submit!", fields);
        }
      });
    },
  },
};
</script>
<!-- <script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  name: "Hello",
  region: "",
  count: "",
  date1: "",
  date2: "",
  delivery: false,
  type: [],
  resource: "",
  desc: "",
});

const rules = reactive<FormRules>({
  name: [
    { required: true, message: "Please input Activity name", trigger: "blur" },
    { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
  ],
  region: [
    {
      required: true,
      message: "Please select Activity zone",
      trigger: "change",
    },
  ],
  count: [
    {
      required: true,
      message: "Please select Activity count",
      trigger: "change",
    },
  ],
  date1: [
    {
      type: "date",
      required: true,
      message: "Please pick a date",
      trigger: "change",
    },
  ],
  date2: [
    {
      type: "date",
      required: true,
      message: "Please pick a time",
      trigger: "change",
    },
  ],
  type: [
    {
      type: "array",
      required: true,
      message: "Please select at least one activity type",
      trigger: "change",
    },
  ],
  resource: [
    {
      required: true,
      message: "Please select activity resource",
      trigger: "change",
    },
  ],
  desc: [
    { required: true, message: "Please input activity form", trigger: "blur" },
  ],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));
</script> -->
